<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.inc.php');
require_once(__PHY_LIB.'/class/uUser.class.php');

/// 跳回地址 ///
$sBackUrl = get_back_url('/');
$oTpl->assign('sBack', $sBackUrl);

/// 初始化 ///
$rReq = validate($_REQUEST, 'action:e[none,bye,check,login]|mode:e[none,ajax]');
$sAction = $rReq['action'];
$rMsg = array();

/// 行为代码 ///
if ('bye' == $sAction) {
	uUser::logout();
	$_The['UserType'] = false;
	header("Location: {$sBackUrl}"); exit();
}
elseif ('check' == $sAction) {
	do {
		$rP = validate($_GET, 'field:u|value:u');
		if ('acct' == $rP['field']) {
			if (!$rP['value']) { print('邮箱为空'); break; }
			if (!strpos($rP['value'], '@')) { print('邮箱错误'); break; }
		}
		elseif ('pswd' == $rP['field']) {
			if (!$rP['value']) { print('密码为空'); break; }
		}
	} while (false);
	exit(); // check状态下，立即输出错误信息
}
elseif ('login' == $sAction) {
	do {
		$rP = validate($_POST, 'acct:u|pswd:u|long:n');
		$oTpl->assign('rP', $rP);
		if (!$rP['acct']) { $rMsg['acct'] = '邮箱为空'; }
		elseif (!strpos($rP['acct'], '@')) { $rMsg['acct'] = '邮箱错误'; }
		if (!$rP['pswd']) { $rMsg['pswd'] = '密码为空'; }
		if (0 < sizeof($rMsg)) { break; }
		if (uUser::login($rP['acct'], $rP['pswd'], $rP['long'])) {
			header("Location: {$sBackUrl}"); exit();
		}
		else {
			$rMsg['acct'] = '登录失败。密码输入错误。';
		}
	} while (false);
}

/// 登录时立即跳转 ///
if ('user' == $_The['UserType'] && 0 < $_The['User']['id']) {
	header("Location: {$sBackUrl}"); exit();
}

$oTpl->assign('rMsg', $rMsg);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').('none' != $rReq['mode'] ? "_{$rReq['mode']}" : '').'.tpl');
?>
